
package view;

import util.BookData;

import com.oahc.bookreaderone.MainActivity;
import com.oahc.bookreaderone.R;

import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BookMenuView implements OnClickListener {
    LinearLayout bookmenu, menu_bright, menu_font, menu_theme, menu_night;

    View fontSetting, themeSetting, animationSetting;

    ImageView font_big, font_small, style_01, style_02, style_03, style_04;

    TextView translateTextView, nullTextView;

    private static BookMenuView instance = null;

    public static BookMenuView getCurrentBookMenuView() {
        return instance;
    }

    int bookid;

    public BookMenuView(View parent) {
        instance = this;
        initView(parent);
    }

    private void initView(View parent) {
        bookmenu = (LinearLayout)parent.findViewById(R.id.book_menu);
        menu_bright = (LinearLayout)parent.findViewById(R.id.menu_bright);
        menu_font = (LinearLayout)parent.findViewById(R.id.menu_font);
        menu_theme = (LinearLayout)parent.findViewById(R.id.menu_theme);
        menu_night = (LinearLayout)parent.findViewById(R.id.menu_read_model);
        // 设置
        fontSetting = parent.findViewById(R.id.book_read_font);
        themeSetting = parent.findViewById(R.id.book_read_theme);
        animationSetting = parent.findViewById(R.id.book_read_animation);
        //
        translateTextView = (TextView)parent.findViewById(R.id.animation_translate);
        nullTextView = (TextView)parent.findViewById(R.id.animation_null);

        font_big = (ImageView)parent.findViewById(R.id.font_big);
        font_small = (ImageView)parent.findViewById(R.id.font_small);
        style_01 = (ImageView)parent.findViewById(R.id.style01);
        style_02 = (ImageView)parent.findViewById(R.id.style02);
        style_03 = (ImageView)parent.findViewById(R.id.style03);
        style_04 = (ImageView)parent.findViewById(R.id.style04);
        // 监听器
        font_big.setOnClickListener(this);
        font_small.setOnClickListener(this);
        style_01.setOnClickListener(this);
        style_02.setOnClickListener(this);
        style_03.setOnClickListener(this);
        style_04.setOnClickListener(this);
        //
        menu_bright.setOnClickListener(this);
        menu_font.setOnClickListener(this);
        menu_theme.setOnClickListener(this);
        menu_night.setOnClickListener(this);
        //
        translateTextView.setOnClickListener(this);
        nullTextView.setOnClickListener(this);
    }

    public void show() {
        if (bookmenu.getVisibility() == View.INVISIBLE) {
            bookmenu.setAnimation(translateAnim(-1, 0));
            bookmenu.setVisibility(View.VISIBLE);
        } else {
            bookmenu.setAnimation(translateAnim(0, 1));
            bookmenu.setVisibility(View.INVISIBLE);
        }
    }

    public void dismiss() {
        bookmenu.setVisibility(View.INVISIBLE);
        themeSetting.setVisibility(View.INVISIBLE);
        fontSetting.setVisibility(View.INVISIBLE);
        animationSetting.setVisibility(View.INVISIBLE);
    }

    public boolean isShown() {
        // TODO Auto-generated method stub
        return bookmenu.isShown();
    }

    public void checkSettingItem() {
        if (themeSetting.isShown()) {
            themeSetting.setAnimation(translateAnim(0, 1));
            themeSetting.setVisibility(View.INVISIBLE);
            bookmenu.setVisibility(View.INVISIBLE);
        } else if (fontSetting.isShown()) {
            fontSetting.setAnimation(translateAnim(0, 1));
            fontSetting.setVisibility(View.INVISIBLE);
            bookmenu.setVisibility(View.INVISIBLE);
        } else if (animationSetting.isShown()) {
            animationSetting.setAnimation(translateAnim(0, 1));
            animationSetting.setVisibility(View.INVISIBLE);
            bookmenu.setVisibility(View.INVISIBLE);
        } else {
            bookmenu.setVisibility(View.INVISIBLE);
        }
    }

    public void onClick(View v) {
        if (v == menu_bright) {
            brightSetting();
        } else if (v == menu_font) {
            fontSetting();
        } else if (v == menu_theme) {
            themeSetting();
        } else if (v == menu_night) {
            nightModeSetting();
        } else if (v == font_big) {
            bookid = BookPageFactory.getInstance().getBookId();
            float fontsize = BookData.getInstance().getFontSize(bookid) + 2;
            if (fontsize < 36) {
                BookData.getInstance().saveFontSize(fontsize, bookid);
                showFontTip(fontsize);
            }
            BookPageFactory.getInstance().refreshBook(false);
        } else if (v == font_small) {
            bookid = BookPageFactory.getInstance().getBookId();
            float fontsize = BookData.getInstance().getFontSize(bookid) - 2;
            if (fontsize > 18) {
                BookData.getInstance().saveFontSize(fontsize, bookid);
                showFontTip(fontsize);
            }
            BookPageFactory.getInstance().refreshBook(false);
        } else if (v == this.style_01) {
            bookid = BookPageFactory.getInstance().getBookId();
            BookData.getInstance().saveTheme(0, bookid);
            BookPageFactory.getInstance().refreshBook(true);
        } else if (v == this.style_02) {
            bookid = BookPageFactory.getInstance().getBookId();
            BookData.getInstance().saveTheme(1, bookid);
            BookPageFactory.getInstance().refreshBook(true);
        } else if (v == this.style_03) {
            bookid = BookPageFactory.getInstance().getBookId();
            BookData.getInstance().saveTheme(2, bookid);
            BookPageFactory.getInstance().refreshBook(true);
        } else if (v == this.style_04) {
            bookid = BookPageFactory.getInstance().getBookId();
            BookData.getInstance().saveTheme(3, bookid);
            BookPageFactory.getInstance().refreshBook(true);
        } else if (v == this.translateTextView) {
            nullTextView.setBackgroundResource(R.drawable.reader_dialog_setting_btn_transparent);
            translateTextView.setBackgroundResource(R.drawable.reader_dialog_setting_btn);
            BookData.getInstance().saveAnimation(true);
        } else if (v == this.nullTextView) {
            nullTextView.setBackgroundResource(R.drawable.reader_dialog_setting_btn);
            translateTextView
                    .setBackgroundResource(R.drawable.reader_dialog_setting_btn_transparent);
            BookData.getInstance().saveAnimation(false);
        }
    }

    private void nightModeSetting() {
        // TODO Auto-generated method stub

    }

    private void themeSetting() {
        // TODO Auto-generated method stub
        themeSetting.setAnimation(translateAnim(-1, 0));
        themeSetting.setVisibility(View.VISIBLE);
    }

    private void fontSetting() {
        // TODO Auto-generated method stub
        fontSetting.setAnimation(translateAnim(-1, 0));
        fontSetting.setVisibility(View.VISIBLE);
    }

    private void brightSetting() {
        // TODO Auto-generated method stub
        animationSetting.setAnimation(translateAnim(-1, 0));
        animationSetting.setVisibility(View.VISIBLE);
    }

    public void showFontTip(float fontsize) {
        Toast toast = Toast.makeText(MainActivity.getInstance(), "当前字体大小：" + fontsize,
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public Animation translateAnim(float fromX, float toX) {
        Animation translateFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, fromX,
                Animation.RELATIVE_TO_PARENT, toX, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0);
        translateFromLeft.setDuration(800);
        return translateFromLeft;
    }

    public void destory() {
        instance = null;
    }

}
